package com.kylin.system.controller;

import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.core.entity.Page;
import com.kylin.common.core.util.DateTimeUtils;
import com.kylin.common.core.validator.Assert;
import com.kylin.common.core.validator.BeanValidator;
import com.kylin.common.log.annotation.Log;
import com.kylin.common.log.enums.BusinessType;
import com.kylin.common.poi.excel.EasyExcelUtil;
import com.kylin.system.entity.SysOperLog;
import com.kylin.system.service.SysOperLogService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 操作日志记录
 * 
 * @author wuhao
 */
@RestController
@RequestMapping("/operlog")
public class SysOperlogController
{
    /** 操作时间数据库字段 */
    private static final String OPER_TIME_FILED = "operTime";

    @Autowired
    private SysOperLogService operLogService;

    /**
     * 操作日志列表查询
     */
//    @PreAuthorize("@ss.hasPermi('system:operlog:list')")
    @GetMapping("/list")
    public ResultBean<Page<SysOperLog>> list(SysOperLog operLog)
    {
        operLog.getQueryWrapper()
                .like(StringUtils.isNotBlank(operLog.getTitle()),"title", operLog.getTitle())
                .like(StringUtils.isNotBlank(operLog.getOperName()), "oper_name", operLog.getOperName())
                .eq(ObjectUtils.isNotEmpty(operLog.getOperatorType()), "operator_type", operLog.getOperatorType())
                .eq(StringUtils.isNotBlank(operLog.getStatus()), "status", operLog.getStatus());


        Page<SysOperLog> page = new Page<>();
        page = operLogService.listPage(page, operLog);
        return ResultBean.onSuccess(page);
    }

    /**
     * 新增操作日志
     */
    @PostMapping
    public ResultBean<Object> add(@RequestBody SysOperLog operLog)
    {
        // 参数校验
        BeanValidator.validate(operLog);
        operLogService.save(operLog);
        return ResultBean.onSuccess();
    }

    /**
     * 删除操作日志
     */
//    @PreAuthorize("@ss.hasPermi('system:operlog:remove')")
    @DeleteMapping("/{operIds}")
    public ResultBean<Object> remove(@PathVariable Long[] operIds)
    {
        Assert.isBlank("请选择需要删除的数据", operIds);
        int count = operLogService.deleteBatchIds(Arrays.asList(operIds));
        return ResultBean.onSuccess().put("count", count);
    }

    /**
     * 清空日志
     */
//    @PreAuthorize("@ss.hasPermi('system:operlog:remove')")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public ResultBean<Object> clean()
    {
        operLogService.cleanOperLog();
        return  ResultBean.onSuccess();
    }



    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:operlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog) throws IOException
    {
        operLog.getQueryWrapper()
                .like(StringUtils.isNotBlank(operLog.getTitle()),"title", operLog.getTitle())
                .like(StringUtils.isNotBlank(operLog.getOperName()), "oper_name", operLog.getOperName())
                .eq(ObjectUtils.isNotEmpty(operLog.getOperatorType()), "operator_type", operLog.getOperatorType())
                .eq(StringUtils.isNotBlank(operLog.getStatus()), "status", operLog.getStatus());
        List<SysOperLog> list = operLogService.list(operLog);
        EasyExcelUtil.exportExcel(list, "操作日志汇总", "操作日志", SysOperLog.class, "操作日志导出", response);
    }






}
